currentsens 2.2.0
Loading...
Searching...
No Matches
currentsens.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
27
28#ifndef CURRENTSENS_H
29#define CURRENTSENS_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_analog_in.h"
52#include "drv_spi_master.h"
53#include "spi_specifics.h"
54
60
65
70
75#define CURRENTSENS_NUM_CONVERSIONS 200
76
81#define CURRENTSENS_ADC_RESOLUTION 0x0FFF
82#define CURRENTSENS_VREF_2V5 2.5
83#define CURRENTSENS_VREF_3V3 3.3
84#define CURRENTSENS_VREF_5V 5.0
85
90#define CURRENTSENS_TIMEOUT_MS 2000
91
100#define CURRENTSENS_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
101#define CURRENTSENS_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
102 // currentsens_set
104
109
114
119#define CURRENTSENS_MAP_MIKROBUS( cfg, mikrobus ) \
120 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
121 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
122 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
123 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
124 cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN );
125 // currentsens_map // currentsens
128
139
144typedef struct
145{
146 analog_in_t adc;
147 spi_master_t spi;
148
149 pin_name_t chip_select;
151 float vref;
154
156
161typedef struct
162{
163 pin_name_t an;
164 pin_name_t miso;
165 pin_name_t mosi;
166 pin_name_t sck;
167 pin_name_t cs;
168
169 uint32_t spi_speed;
170 spi_master_mode_t spi_mode;
171 spi_master_chip_select_polarity_t cs_polarity;
172
173 analog_in_resolution_t resolution;
174 float vref;
176
178
189
195
206
222
237
249err_t currentsens_read_raw_adc ( currentsens_t *ctx, uint16_t *raw_adc );
250
262err_t currentsens_read_voltage ( currentsens_t *ctx, float *voltage );
263
276err_t currentsens_read_voltage_avg ( currentsens_t *ctx, uint16_t num_conv, float *voltage_avg );
277
289err_t currentsens_set_vref ( currentsens_t *ctx, float vref );
290
302
315err_t currentsens_calib_resolution ( currentsens_t *ctx, float calib_current );
316
329err_t currentsens_read_current ( currentsens_t *ctx, float *current );
330
331#ifdef __cplusplus
332}
333#endif
334#endif // CURRENTSENS_H
335 // currentsens
337
338// ------------------------------------------------------------------------ END
currentsens_drv_t
Current Sens Click driver selector.
Definition currentsens.h:134
@ CURRENTSENS_DRV_SEL_SPI
Definition currentsens.h:136
@ CURRENTSENS_DRV_SEL_ADC
Definition currentsens.h:135
currentsens_return_value_t
Current Sens Click return value data.
Definition currentsens.h:184
@ CURRENTSENS_OK
Definition currentsens.h:185
@ CURRENTSENS_ERROR
Definition currentsens.h:186
err_t currentsens_calib_offset(currentsens_t *ctx)
Current Sens calib offset function.
err_t currentsens_set_vref(currentsens_t *ctx, float vref)
Current Sens set vref function.
err_t currentsens_calib_resolution(currentsens_t *ctx, float calib_current)
Current Sens calib resolution function.
err_t currentsens_read_current(currentsens_t *ctx, float *current)
Current Sens read current function.
void currentsens_cfg_setup(currentsens_cfg_t *cfg)
Current Sens configuration object setup function.
err_t currentsens_init(currentsens_t *ctx, currentsens_cfg_t *cfg)
Current Sens initialization function.
void currentsens_drv_interface_sel(currentsens_cfg_t *cfg, currentsens_drv_t drv_sel)
Current Sens driver interface setup function.
err_t currentsens_read_voltage(currentsens_t *ctx, float *voltage)
Current Sens read voltage level function.
err_t currentsens_read_voltage_avg(currentsens_t *ctx, uint16_t num_conv, float *voltage_avg)
Current Sens read average voltage level function.
err_t currentsens_read_raw_adc(currentsens_t *ctx, uint16_t *raw_adc)
Current Sens read raw ADC value function.
This file contains SPI specific macros, functions, etc.
Current Sens Click configuration object.
Definition currentsens.h:162
analog_in_resolution_t resolution
Definition currentsens.h:173
float vref
Definition currentsens.h:174
currentsens_drv_t drv_sel
Definition currentsens.h:175
spi_master_chip_select_polarity_t cs_polarity
Definition currentsens.h:171
pin_name_t sck
Definition currentsens.h:166
spi_master_mode_t spi_mode
Definition currentsens.h:170
pin_name_t mosi
Definition currentsens.h:165
uint32_t spi_speed
Definition currentsens.h:169
pin_name_t miso
Definition currentsens.h:164
pin_name_t an
Definition currentsens.h:163
pin_name_t cs
Definition currentsens.h:167
Current Sens Click context object.
Definition currentsens.h:145
spi_master_t spi
Definition currentsens.h:147
float data_resolution
Definition currentsens.h:153
float vref
Definition currentsens.h:151
currentsens_drv_t drv_sel
Definition currentsens.h:150
float zero_curr_offset
Definition currentsens.h:152
pin_name_t chip_select
Definition currentsens.h:149
analog_in_t adc
Definition currentsens.h:146